<template>
	<view class="container">
		<!-- 地址列表 -->
		<scroll-view class="address-list" scroll-y="true">
			<view class="address-item" v-for="(address, index) in addressList" :key="index">
				<view class="address-content" @click="selectAddress(address)">
					<view class="address-left">
						<view class="contact-info">
							<text class="name">{{address.name}}</text>
							<text class="phone">{{address.phone}}</text>
							<view class="default-tag" v-if="address.isDefault">默认</view>
						</view>
						<view class="address-text">{{address.province}}{{address.city}}{{address.district}}{{address.detail}}</view>
					</view>
					<view class="address-right">
						<view class="edit-btn" @click.stop="editAddress(address)">编辑</view>
					</view>
				</view>
				<view class="address-actions">
					<view class="action-item" @click="setDefault(address, index)">
						<view class="action-icon">⭐</view>
						<text class="action-text">{{address.isDefault ? '默认地址' : '设为默认'}}</text>
					</view>
					<view class="action-item" @click="deleteAddress(index)">
						<view class="action-icon">🗑️</view>
						<text class="action-text">删除</text>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 底部添加按钮 -->
		<view class="bottom-actions">
			<button class="add-address-btn" @click="addNewAddress">+ 新增收货地址</button>
		</view>

		<!-- 地址编辑弹窗 -->
		<view class="edit-modal" v-if="showEditModal" @click="hideEditModal">
			<view class="modal-content" @click.stop>
				<view class="modal-header">
					<text class="modal-title">{{editingAddress ? '编辑地址' : '新增地址'}}</text>
					<view class="close-btn" @click="hideEditModal">×</view>
				</view>
				<view class="form-content">
					<view class="form-item">
						<text class="form-label">收货人</text>
						<input class="form-input" v-model="formData.name" placeholder="请输入收货人姓名" />
					</view>
					<view class="form-item">
						<text class="form-label">手机号码</text>
						<input class="form-input" v-model="formData.phone" placeholder="请输入手机号码" type="number" maxlength="11" />
					</view>
					<view class="form-item">
						<text class="form-label">所在地区</text>
						<picker class="form-picker" mode="region" @change="onRegionChange">
							<view class="picker-text">{{formData.regionText || '请选择所在地区'}}</view>
						</picker>
					</view>
					<view class="form-item">
						<text class="form-label">详细地址</text>
						<textarea class="form-textarea" v-model="formData.detail" placeholder="请输入详细地址" />
					</view>
					<view class="form-item">
						<text class="form-label">设为默认地址</text>
						<switch class="form-switch" :checked="formData.isDefault" @change="onDefaultChange" />
					</view>
				</view>
				<view class="modal-actions">
					<button class="save-btn" @click="saveAddress">保存</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showEditModal: false,
				editingAddress: null,
				formData: {
					name: '',
					phone: '',
					province: '',
					city: '',
					district: '',
					detail: '',
					regionText: '',
					isDefault: false
				},
				addressList: [
					{
						id: 1,
						name: '张三',
						phone: '18790784768',
						province: '湖南省',
						city: '长沙市',
						district: '岳麓区',
						detail: '某某街道某某小区1栋101室',
						isDefault: true
					},
					{
						id: 2,
						name: '李四',
						phone: '13800138000',
						province: '广东省',
						city: '深圳市',
						district: '南山区',
						detail: '科技园路1号',
						isDefault: false
					}
				]
			}
		},
		methods: {
			selectAddress(address) {
				// 选择地址，返回上一页
				uni.$emit('addressSelected', address);
				uni.navigateBack();
			},
			editAddress(address) {
				this.editingAddress = address;
				this.formData = {
					name: address.name,
					phone: address.phone,
					province: address.province,
					city: address.city,
					district: address.district,
					detail: address.detail,
					regionText: address.province + address.city + address.district,
					isDefault: address.isDefault
				};
				this.showEditModal = true;
			},
			addNewAddress() {
				this.editingAddress = null;
				this.formData = {
					name: '',
					phone: '',
					province: '',
					city: '',
					district: '',
					detail: '',
					regionText: '',
					isDefault: false
				};
				this.showEditModal = true;
			},
			hideEditModal() {
				this.showEditModal = false;
				this.editingAddress = null;
			},
			onRegionChange(e) {
				const [province, city, district] = e.detail.value;
				this.formData.province = province;
				this.formData.city = city;
				this.formData.district = district;
				this.formData.regionText = province + city + district;
			},
			onDefaultChange(e) {
				this.formData.isDefault = e.detail.value;
			},
			saveAddress() {
				if (!this.formData.name || !this.formData.phone || !this.formData.regionText || !this.formData.detail) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					});
					return;
				}
				
				if (this.editingAddress) {
					// 编辑地址
					const index = this.addressList.findIndex(item => item.id === this.editingAddress.id);
					if (index !== -1) {
						this.addressList[index] = { ...this.editingAddress, ...this.formData };
					}
				} else {
					// 新增地址
					const newAddress = {
						id: Date.now(),
						...this.formData
					};
					this.addressList.push(newAddress);
				}
				
				// 如果设为默认，取消其他地址的默认状态
				if (this.formData.isDefault) {
					this.addressList.forEach(item => {
						if (item.id !== (this.editingAddress ? this.editingAddress.id : newAddress.id)) {
							item.isDefault = false;
						}
					});
				}
				
				this.hideEditModal();
				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});
			},
			setDefault(address, index) {
				if (address.isDefault) return;
				
				this.addressList.forEach(item => {
					item.isDefault = false;
				});
				this.addressList[index].isDefault = true;
				
				uni.showToast({
					title: '设置成功',
					icon: 'success'
				});
			},
			deleteAddress(index) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这个地址吗？',
					success: (res) => {
						if (res.confirm) {
							this.addressList.splice(index, 1);
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

/* 地址列表 */
.address-list {
	padding: 20rpx;
}

.address-item {
	background-color: #fff;
	border-radius: 20rpx;
	margin-bottom: 20rpx;
	overflow: hidden;
}

.address-content {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.address-left {
	flex: 1;
}

.contact-info {
	display: flex;
	align-items: center;
	gap: 20rpx;
	margin-bottom: 15rpx;
}

.name {
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
}

.phone {
	font-size: 26rpx;
	color: #666;
}

.default-tag {
	background-color: #ff6b35;
	color: white;
	padding: 4rpx 12rpx;
	border-radius: 12rpx;
	font-size: 20rpx;
}

.address-text {
	font-size: 26rpx;
	color: #333;
	line-height: 1.4;
}

.address-right {
	display: flex;
	align-items: center;
}

.edit-btn {
	color: #ff6b35;
	font-size: 26rpx;
	padding: 10rpx 20rpx;
	border: 1rpx solid #ff6b35;
	border-radius: 20rpx;
}

.address-actions {
	display: flex;
	padding: 20rpx 30rpx;
	gap: 40rpx;
}

.action-item {
	display: flex;
	align-items: center;
	gap: 10rpx;
}

.action-icon {
	font-size: 32rpx;
}

.action-text {
	font-size: 24rpx;
	color: #666;
}

/* 底部添加按钮 */
.bottom-actions {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: #fff;
	padding: 20rpx 30rpx;
	border-top: 1rpx solid #f0f0f0;
}

.add-address-btn {
	width: 100%;
	background-color: #ff6b35;
	color: white;
	border: none;
	padding: 25rpx;
	border-radius: 25rpx;
	font-size: 32rpx;
	font-weight: bold;
}

/* 地址编辑弹窗 */
.edit-modal {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 1000;
	display: flex;
	align-items: center;
	justify-content: center;
}

.modal-content {
	background-color: #fff;
	width: 90%;
	max-height: 80vh;
	border-radius: 20rpx;
	overflow: hidden;
}

.modal-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.modal-title {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
}

.close-btn {
	font-size: 40rpx;
	color: #999;
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.form-content {
	padding: 30rpx;
}

.form-item {
	display: flex;
	align-items: center;
	padding: 25rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
}

.form-item:last-child {
	border-bottom: none;
}

.form-label {
	width: 200rpx;
	font-size: 28rpx;
	color: #333;
}

.form-input, .form-picker, .form-textarea {
	flex: 1;
	font-size: 28rpx;
	color: #333;
}

.form-input {
	padding: 15rpx 0;
}

.picker-text {
	padding: 15rpx 0;
	color: #333;
}

.form-textarea {
	padding: 15rpx 0;
	min-height: 80rpx;
}

.form-switch {
	transform: scale(0.8);
}

.modal-actions {
	padding: 30rpx;
	border-top: 1rpx solid #f0f0f0;
	text-align: center;
}

.save-btn {
	background-color: #ff6b35;
	color: white;
	border: none;
	padding: 25rpx 60rpx;
	border-radius: 25rpx;
	font-size: 32rpx;
	font-weight: bold;
}
</style>